package com.gdufe.firesafe.controller;

import com.gdufe.firesafe.annotation.NeedInspector;
import com.gdufe.firesafe.annotation.NeedOwner;
import com.gdufe.firesafe.model.dto.DeviceDTO;
import com.gdufe.firesafe.model.entity.DeviceEntity;
import com.gdufe.firesafe.model.vo.DeviceVO;
import com.gdufe.firesafe.service.DeviceService;
import com.gdufe.firesafe.utils.PageUtils;
import com.gdufe.firesafe.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 消防设备表
 *
 * @author Liu
 * @email
 * @date 2024-03-25 20:54:21
 */
@RestController
@RequestMapping("device/device")
public class DeviceController {
    @Autowired
    private DeviceService deviceService;

    /**
     * 获取业主房屋的设备
     * @return
     */
    @NeedOwner
    @GetMapping("/getOwnerDevice")
    public R getOwnerDevice(){
        List<DeviceVO> deviceVOList = deviceService.getOwnerDevice();
        return R.ok().put("ownerDevice", deviceVOList);
    }

    /**
     * 列表
     */
    @NeedInspector //检查员和管理员可用
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = deviceService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		DeviceEntity device = deviceService.getById(id);
        //实体转DTO
        DeviceDTO deviceDTO = DeviceDTO.entityToDTO(device);
        return R.ok().put("device", deviceDTO);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody DeviceDTO deviceDTO){
        // 校验数据
        deviceService.validateAddOrUpdate(deviceDTO, true);
        //新增设备
        deviceService.addDevice(deviceDTO);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody DeviceDTO deviceDTO){
        // 校验数据
        deviceService.validateAddOrUpdate(deviceDTO, false);
		deviceService.updateDevice(deviceDTO);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		deviceService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
